package com.jp.knowledge.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jp.knowledge.R;
import com.jp.knowledge.logic.TopicLogic;
import com.jp.knowledge.logic.TopicLogicCallBack;
import com.jp.knowledge.model.TopicListInfo;
import com.jp.knowledge.util.ImageLoadUtil;
import com.jp.knowledge.util.L;
import com.jp.knowledge.util.TimeUtil;
import com.umeng.socialize.utils.Log;

import java.util.List;
import java.util.Random;

/**
 * Created by jiayen on 16/10/12.
 */

public  class TopicAdpter extends  RecyclerView.Adapter<TopicAdpter.Viewholder> implements View.OnClickListener {
    private int size;
    private OnItemListerner onItemListener;
    private Context mContext;
    private List<TopicListInfo>  list;

    private TopicLogic topicLogic;

    private String topicId;
    private String infoId;
    public TopicAdpter(Context context, final List<TopicListInfo>  list){
            this.mContext=context;
        this.list=list;
        size =list==null?0:list.size();

    }

    public void setInfoId(String infoId) {
        this.infoId = infoId;
    }

    public void setTopicId(String topicId) {
        this.topicId = topicId;
    }

    public void setOnItemListener(OnItemListerner onItemListener) {
        this.onItemListener = onItemListener;
    }

    public void update( final List<TopicListInfo>  data){
        this.list=data;
        notifyDataSetChanged();

        if(topicLogic==null)
            topicLogic= new TopicLogic(mContext,new TopicLogicCallBack() {
                @Override
                public void priseSuccess(int position) {
                    if(data!=null){
                        data.get(position).setIsPraise(1);
                        data.get(position).setPraiseNum( data.get(position).getPraiseNum()+1);
                        update(data);
                    }
                }
            });

    }

    public void removeItem(int position){
        list.remove(position);
        notifyItemRemoved(position);

    }
    /**
     * item显示类型
     * @param parent
     * @param viewType
     * @return
     */
    @Override
    public TopicAdpter.Viewholder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.topic_item,parent,false);
        Viewholder  viewholder = new Viewholder(view);
        onItemClickEvent(viewholder);
        L.e("onCreateViewHolder===viewType=="+viewType);
        return viewholder;
    }

    /**
     * item点击事件
     * @param viewholder
     */
    private void onItemClickEvent(Viewholder viewholder) {
        if(onItemListener!=null){
            viewholder.itemView.setOnClickListener(this);
        }
    }

    /**
     * item数据绑定
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(TopicAdpter.Viewholder holder, int position) {

        TopicListInfo info = list.get(position);
        holder.itemView.setTag(position);
        holder.name.setText(info.getNickName());
//        Glide.with(context).load(info.getPortrait()).bitmapTransform(new CropCircleTransformation(context))
//                .fitCenter().placeholder(R.mipmap.toutiaoxiangqingmo)
//                .override(30,30).into(holder.photo);//new BlurTransformation(getContext(), 25),
        ImageLoadUtil.loadCircle(mContext,info.getPortrait(),holder.photo);

        holder.prise.setText(info.getPraiseNum()+"");
        holder.prise.setTag(position);

        if(info.getIsPraise()==1){//说明是点赞的
            holder.prise.setCompoundDrawablesWithIntrinsicBounds(mContext.getResources().getDrawable(R.mipmap.toutiaoxiangqingyidianzan),null,null,null);
        }else{//说明是没有点赞的
            holder.prise.setOnClickListener(this);
            holder.prise.setCompoundDrawablesWithIntrinsicBounds(mContext.getResources().getDrawable(R.mipmap.toutiaoxiangqingzan),null,null,null);
        }


        holder.content.setText(info.getContent());

        TopicListInfo.ReplyDataBean  replyDataBean = info.getReplyData();
        if(replyDataBean!=null){
            String replyContent = replyDataBean.getContent();
            if(replyContent!=null&&!"".equals(replyContent.trim())&&!"null".equals(replyContent.trim())&&info.getReplyNum()>0){
                holder.replyLayout.setVisibility(View.VISIBLE);
                holder.replyName.setText(replyDataBean.getUserName());
                holder.replyContent.setText(replyContent);
                holder.moreReply.setText("更多"+info.getReplyNum()+"条回复");
            }
        }else{
            holder.replyLayout.setVisibility(View.GONE);
        }

        holder.time.setText(TimeUtil.friendly_time(info.getTime()));
    }


    private String getName(){
        Random random =new Random();
        int i = random.nextInt(100);
        return "老巫"+i;
    }



    @Override
    public int getItemCount() {
        return list==null?0:list.size();
    }

    @Override
    public void onClick(View view) {
        view.getParent();

        int position = (int) view.getTag();
        Log.e("onClick==position="+position);
        if(view.getId()==R.id.prise){//点赞
            TopicListInfo info = list.get(position);
            String infoId = info.getInfoId();
            if(TextUtils.isEmpty(infoId))infoId=this.infoId;
            topicLogic.topicPrise(infoId,topicId,info.getCommentId(),position);
        }else {//itemOnclick
            onItemListener.onItemClick(view, position);
        }

    }

    public static  class  Viewholder extends  RecyclerView.ViewHolder{
        public TextView  name;//名字
        public TextView prise;//点赞
        public TextView talk;//回复
        public TextView content;//内容
        public ImageView photo;//头像
        public RelativeLayout replyLayout;
        public TextView replyName;
        public  TextView replyContent;
        public TextView moreReply;
        public TextView  time;
        public Viewholder(View itemView) {
            super(itemView);
            name = (TextView) itemView.findViewById(R.id.name);
            prise =(TextView) itemView.findViewById(R.id.prise);
            talk =(TextView) itemView.findViewById(R.id.talk);
            photo =(ImageView) itemView.findViewById(R.id.photo);
            content= (TextView) itemView.findViewById(R.id.content);
            replyLayout= (RelativeLayout) itemView.findViewById(R.id.reply);
            replyName= (TextView) itemView.findViewById(R.id.reply_name);
            replyContent= (TextView) itemView.findViewById(R.id.reply_content);
            moreReply= (TextView) itemView.findViewById(R.id.more_reply);
            time= (TextView) itemView.findViewById(R.id.time);
        }
    }


}